﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ListOfTale
{
    /// <summary>
    /// Interaction logic for AuthorForm.xaml
    /// </summary>
    public partial class AuthorForm : Window
    {
        public AuthorForm()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            ListOfTale.TalesDataSet talesDataSet = ((ListOfTale.TalesDataSet)(this.FindResource("talesDataSet")));
            ListOfTale.TalesDataSetTableAdapters.AuthorTableAdapter talesDataSetAuthorTableAdapter = new ListOfTale.TalesDataSetTableAdapters.AuthorTableAdapter();
            talesDataSetAuthorTableAdapter.Fill(talesDataSet.Author);
            System.Windows.Data.CollectionViewSource authorViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("authorViewSource")));
            authorViewSource.View.MoveCurrentToFirst();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Вы уверены?", "", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                if (authorDataGrid.SelectedItem != null)
                {
                    var val = ((authorDataGrid.SelectedItem as System.Data.DataRowView).Row as TalesDataSet.AuthorRow);
                    if (val != null)
                    {

                        TalesDataSetTableAdapters.AuthorTableAdapter ta = new TalesDataSetTableAdapters.AuthorTableAdapter();
                        ta.Delete(val.Language, val.Name, val.AuthorId);
                        Window_Loaded(this, new RoutedEventArgs());
                    }
                }
                else MessageBox.Show("Нужно выбрать строку");
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (authorDataGrid.SelectedItem != null)
            {
                var val = ((authorDataGrid.SelectedItem as System.Data.DataRowView).Row as TalesDataSet.AuthorRow);
                AddAuth aa = new AddAuth(false, val.Name, val.Language, val.AuthorId);
                if ( aa.ShowDialog().Value)
                {
                    Window_Loaded(this, new RoutedEventArgs());
                }
            }
            else MessageBox.Show("Нужно выбрать строку");
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if (authorDataGrid.SelectedItem != null)
            {
                var val = ((authorDataGrid.SelectedItem as System.Data.DataRowView).Row as TalesDataSet.AuthorRow);
                AddAuth aa = new AddAuth(true);
                if (aa.ShowDialog().Value)
                {
                    Window_Loaded(this, new RoutedEventArgs());
                }
            }
            else MessageBox.Show("Нужно выбрать строку");
        }



        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            List<int> ids = new List<int>();
            if (authorDataGrid.SelectedItems.Count <= 1)
            {
                MessageBox.Show("Нужно выбрать авторов");
                return;
            }
            
            foreach (var s in authorDataGrid.SelectedItems)
            {
                TalesDataSet.AuthorRow r = (s as System.Data.DataRowView).Row as  TalesDataSet.AuthorRow;
                ids.Add(r.AuthorId);
            }
            int orid = ids[0];
            ids.Remove(orid);

            TalesDataSetTableAdapters.TaleTableAdapter qta = new TalesDataSetTableAdapters.TaleTableAdapter();
            TalesDataSetTableAdapters.AuthorTableAdapter ta = new TalesDataSetTableAdapters.AuthorTableAdapter();
            qta.Connection = ta.Connection;
            ta.Connection.Open();
            var tran = ta.Connection.BeginTransaction();
            try
            {
                foreach (var id in ids)
                {
                    qta.UpdateAuthor(orid, id);
                    ta.DeleteQuery(id);
                }
                tran.Commit();
                ta.Connection.Close();
            }
            catch { tran.Rollback(); ta.Connection.Close(); }
            Window_Loaded(this, new RoutedEventArgs());
        }
    }
}
